package com.school.sports.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.school.sports.entity.Backup;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 备份Mapper接口
 * 处理数据库备份相关的数据访问操作
 */
@Mapper
public interface BackupMapper extends BaseMapper<Backup> {

    /**
     * 分页查询备份记录
     * @param page 分页参数
     * @param backupName 备份名称（可选）
     * @param backupType 备份类型（可选）
     * @param operatorId 操作人ID（可选）
     * @param startTime 开始时间（可选）
     * @param endTime 结束时间（可选）
     * @return 分页结果
     */
    IPage<Backup> selectBackupPage(Page<Backup> page,
                                  @Param("backupName") String backupName,
                                  @Param("backupType") String backupType,
                                  @Param("operatorId") Long operatorId,
                                  @Param("startTime") LocalDateTime startTime,
                                  @Param("endTime") LocalDateTime endTime);

    /**
     * 根据操作人查询备份记录
     * @param operatorId 操作人ID
     * @return 备份记录列表
     */
    @Select("SELECT b.*, u.real_name as operator_name " +
            "FROM backup b " +
            "LEFT JOIN user u ON b.operator_id = u.id " +
            "WHERE b.operator_id = #{operatorId} " +
            "ORDER BY b.backup_at DESC")
    List<Backup> selectByOperatorId(@Param("operatorId") Long operatorId);

    /**
     * 根据备份类型查询备份记录
     * @param backupType 备份类型
     * @return 备份记录列表
     */
    @Select("SELECT b.*, u.real_name as operator_name " +
            "FROM backup b " +
            "LEFT JOIN user u ON b.operator_id = u.id " +
            "WHERE b.backup_type = #{backupType} " +
            "ORDER BY b.backup_at DESC")
    List<Backup> selectByBackupType(@Param("backupType") String backupType);

    /**
     * 查询最近的备份记录
     * @param limit 限制数量
     * @return 备份记录列表
     */
    @Select("SELECT b.*, u.real_name as operator_name " +
            "FROM backup b " +
            "LEFT JOIN user u ON b.operator_id = u.id " +
            "ORDER BY b.backup_at DESC " +
            "LIMIT #{limit}")
    List<Backup> selectRecentBackups(@Param("limit") Integer limit);

    /**
     * 统计指定时间范围内的备份数量
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return 备份数量
     */
    @Select("SELECT COUNT(*) FROM backup " +
            "WHERE backup_at >= #{startTime} AND backup_at <= #{endTime}")
    Integer countBackupsByTimeRange(@Param("startTime") LocalDateTime startTime,
                                   @Param("endTime") LocalDateTime endTime);

    /**
     * 根据备份名称查询备份记录
     * @param backupName 备份名称
     * @return 备份记录
     */
    @Select("SELECT b.*, u.real_name as operator_name " +
            "FROM backup b " +
            "LEFT JOIN user u ON b.operator_id = u.id " +
            "WHERE b.backup_name = #{backupName}")
    Backup selectByBackupName(@Param("backupName") String backupName);
}